/**
 * 
 */
package fr.fermeManager.application.views.models;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.DefaultComboBoxModel;

import fr.fermeManager.library.model.beans.Transaction;
import fr.fermeManager.library.model.beans.TypeTransaction;

/**
 * @author Mo
 *
 */
public class ModelComboBoxTypeTransaction extends DefaultComboBoxModel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<TypeTransaction> listeTypesTransac;
	
	private Transaction currentTransaction = new Transaction();
	
	public ModelComboBoxTypeTransaction() {
		super();
	}
	
	public ModelComboBoxTypeTransaction(List<TypeTransaction> listeTypesTransacParam) {
		super();
		Collections.sort(listeTypesTransacParam, new Comparator<TypeTransaction>() {
			@Override
			public int compare(TypeTransaction o1, TypeTransaction o2) {
				if(o1.isEstSortie()!=o2.isEstSortie()){
					if(o1.isEstSortie()){
						return 1;
					}else{
						return -1;
					}
				}else{
					return new Integer(o1.getLevel()).compareTo(new Integer(o2.getLevel()));
				}
			}
		});
		listeTypesTransac = listeTypesTransacParam;
	}
	
	public ModelComboBoxTypeTransaction(List<TypeTransaction> listeTypesTransacParam,Transaction transacSelecParam) {
		super();
		Collections.sort(listeTypesTransacParam, new Comparator<TypeTransaction>() {
			@Override
			public int compare(TypeTransaction o1, TypeTransaction o2) {
				if(o1.isEstSortie()!=o2.isEstSortie()){
					if(o1.isEstSortie()){
						return 1;
					}else{
						return -1;
					}
				}else{
					return new Integer(o1.getLevel()).compareTo(new Integer(o2.getLevel()));
				}
			}
		});
		
		listeTypesTransac = listeTypesTransacParam;
		currentTransaction = transacSelecParam;
	}

	@Override
	public int getSize() {
		return listeTypesTransac.size();
	}

	@Override
	public Object getElementAt(int index) {
		return listeTypesTransac.get(index);
	}

	@Override
	public int getIndexOf(Object anObject) {
		return listeTypesTransac.indexOf(anObject);
	}

	@Override
	public void setSelectedItem(Object anObject) {
		currentTransaction.setTypeTransaction((TypeTransaction) anObject);
	}

	@Override
	public Object getSelectedItem() {
		if(currentTransaction==null){
			return null;
		}
		return currentTransaction.getTypeTransaction();
	}
}
